package com.molihub.utils;

import com.baidu.aip.nlp.AipNlp;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * @ClassName: TextErrorCorrection
 * @Description: 文本纠错工具类
 * @author: GGBOY
 * @date 2020/1/19 19:51
 * @Version: 1.0
 **/
public class TextErrorCorrection {
    private static AipNlp client = null;

    static {
        //初始化一个AipNlp
        client = new AipNlp(TextCheckUtil.APP_ID, TextCheckUtil.API_KEY, TextCheckUtil.SECRET_KEY);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
    }

    /**
     * 对文件进行纠错
     *
     * @param text 需要纠错的文本
     * @return java.lang.String 返回正确的文本
     * @author GGBOY
     * @date 2020/1/19
     */
    public static String correction(String text) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();

        // 文本纠错
        JSONObject res = client.ecnet(text, options);
        JSONObject item = res.getJSONObject("item");
        Object correct_query = item.get("correct_query");
        return correct_query.toString();
    }

}
